package rlog

import (
	"fmt"
	"io"
	"time"
)

var ShowDebug bool = true
var ShowInfo bool = true
var ShowWarrning bool = true
var ShowError bool = true
var ShowCrucial bool = true

var ShowOnConsole bool = true

var TimeLayout string = "2006-01-02 15:04:05.0000"

var target []io.Writer = make([]io.Writer, 0)

func AddOutputer(outputer io.Writer) {
	target = append(target, outputer)
}
func ClearOutputer() {
	target = make([]io.Writer, 0)
}
func GetAllOutputer() []io.Writer {
	return target
}

func curtime() string {
	return time.Now().Format(TimeLayout)
}

func output(level string, message string, arg []interface{}) {
	text := curtime() + " [" + level + "] " + fmt.Sprintf(message, arg...)
	if text[len(text)-1] != '\n' {
		text += "\n"
	}
	for _, v := range target {
		io.WriteString(v, text)
	}

	if ShowOnConsole {
		col := 37
		if level == "Debug" {
			col = 30
		}
		if level == "Warrning" {
			col = 33
		}
		if level == "Error" {
			col = 31
		}
		if level == "Crucial" {
			col = 41
		}
		text = text[0 : len(text)-1]
		fmt.Printf("\x1b[0;%dm%s\x1b[0m\n", col, text)
	}
}

func Debugf(message string, arg ...interface{}) {
	if !ShowDebug {
		return
	}
	output("Debug", message, arg)
}

func Infof(message string, arg ...interface{}) {
	if !ShowInfo {
		return
	}
	output("Info", message, arg)
}

func Warrningf(message string, arg ...interface{}) {
	if !ShowWarrning {
		return
	}
	output("Warrning", message, arg)
}

func Errorf(message string, arg ...interface{}) {
	if !ShowError {
		return
	}
	output("Error", message, arg)
}

func Crucialf(message string, arg ...interface{}) {
	if !ShowCrucial {
		return
	}
	output("Crucial", message, arg)
}
